<!DOCTYPE html>

<head>
    <meta charset="utf-8" />
    <script src="../js/crypto-js.js"></script>
    <script src="../js/sm2.js"></script>
    <script src="../js/jquery-3.4.1.min.js"></script>
    <script>

        $(document).ready(function () {
            $("#submit").click(function () {
                var username = $("#username").val();
                var password = $("#password").val();
                var jsonReqParams = {

                };
                jsonReqParams.username=username;
                jsonReqParams.password=password;
                jsonReqParams=JSON.stringify(jsonReqParams);
                //sm2公钥
                var pubkeyHex = "045417fe9233682d37355d2f64db75fb6ff1146efb68cf409cdd64bb7e31a448047c337b0edd919501f678181a230d91e621029bc64faf7fca3631ac2c869673b9";
                //对请求报文加密
                jsonReqParams=sm2Encrypt(jsonReqParams, pubkeyHex, 0);
                //发送请求到后端
                $.ajax({
                    type: 'POST', //方法类型
                    dataType: 'json', //预期服务器返回的数据类型
                    url: '/login', //请求地址
                    data: { 'jsonReqParams': jsonReqParams },
                    contentType: 'application/x-www-form-urlencoded; charset=utf-8',
                    success: function (data) {
                        if(data.code == "1001"){
                            $("#result").html("登录成功");
                        }else if(data.code== "1002"){
                            $("#result").html("登录失败");
                        }else{
                            $("#result").html("出现异常，请联系管理员");
                        }
                    },
                    error: function () {
                        $("#result").html("出现异常，请联系管理员");
                    },
                });
            });
        });

    </script>
</head>

<body>

    用户名：<input type="text" id="username" /><br />
    密码：<input type="password" id="password" /><br />
    <button id="submit">登录</button><br />
    <span id="result"></span>


</body>